package com.example.shortlink.dao.mapper;

import com.example.shortlink.dao.entity.ShortLink;
import com.example.shortlink.dto.req.ShortLinkPageReqDTO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 短链接持久层
 */
public interface ShortLinkMapper extends BaseMapper<ShortLink> {
    @Select("<script>" +
            "SELECT " +
            "t.id," +
            "t.domain," +
            "t.short_uri," +
            "t.full_short_url," +
            "t.origin_url," +
            "t.click_num," +
            "t.gid," +
            "t.favicon," +
            "t.enable_status," +
            "t.created_type," +
            "t.valid_date_type," +
            "t.valid_date," +
            "t.`describe`," +
            "t.total_pv," +
            "t.total_uv," +
            "t.total_uip," +
            "t.create_time," +
            "t.update_time," +
            "COALESCE ( s.today_pv, 0 ) AS todayPv," +
            "COALESCE ( s.today_uv, 0 ) AS todayUv," +
            "COALESCE ( s.today_uip, 0 ) AS todayUip " +
            "FROM t_link t " +
            "LEFT JOIN t_link_stats_today s ON t.gid = s.gid " +
            "AND t.full_short_url = s.full_short_url " +
            "AND s.date = CURDATE() " +
            "<where>" +
            "t.gid = #{gid} " +
            "AND t.enable_status = 0 " +
            "AND t.del_flag = 0 " +
            "<choose>" +
            "<when test=\"orderTag == 'todayPv'\">" +
            "    ORDER BY todayPv DESC" +
            "</when>" +
            "<when test=\"orderTag == 'todayUv'\">" +
            "    ORDER BY todayUv DESC" +
            "</when>" +
            "<when test=\"orderTag == 'todayUip'\">" +
            "    ORDER BY todayUip DESC" +
            "</when>" +
            "<when test=\"orderTag == 'totalPv'\">" +
            "    ORDER BY t.total_pv DESC" +
            "</when>" +
            "<when test=\"orderTag == 'totalUv'\">" +
            "    ORDER BY t.total_uv DESC" +
            "</when>" +
            "<when test=\"orderTag == 'totalUip'\">" +
            "    ORDER BY t.total_uip DESC" +
            "</when>" +
            "<otherwise>" +
            "    ORDER BY t.create_time DESC" +
            "</otherwise>" +
            "</choose>" +
            "</where>" +
            "</script>")
    Page<ShortLink> pageLink(ShortLinkPageReqDTO requestParam);

    @Update("<script>" +
            "update " +
            "t_link " +
            "set " +
            "total_pv = total_pv + #{totalPv}, " +
            "total_uv = total_uv + #{totalUv}, " +
            "total_uip = total_uip + #{totalUip} " +
            "where " +
            "gid = #{gid} " +
            "and full_short_url = #{fullShortUrl} " +
            "</script>")
    void incrementStats(
            @Param("gid") String gid,
            @Param("fullShortUrl") String fullShortUrl,
            @Param("totalPv") Integer totalPv,
            @Param("totalUv") Integer totalUv,
            @Param("totalUip") Integer totalUip
    );
}